package com.wc.算法提高课.B第二章_搜索.DFS之搜索顺序.单词接龙;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/9/26 23:01
 * @description https://www.acwing.com/problem/content/1119/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 22, M = 500;
    // w[i] 表示连接长度
    static int[] h = new int[N], w = new int[M], ne = new int[M], e = new int[M];
    static int[] used = new int[N];
    static String[] word = new String[N];
    static int n, idx = 1, res = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) word[i] = sc.next();
        word[0] = sc.next();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
//                if (i == j) continue;
                int len = Math.min(word[i].length(), word[j].length());
                for (int k = 1; k <= len; k++) {
                    if (word[i].substring(word[i].length() - k).equals(word[j].substring(0, k))) {
                        add(i, j, word[j].length() - k);
                        break;
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (word[i].charAt(0) == word[0].charAt(0)) {
                add(0, i, word[i].length());
            }
        }
        dfs(0, 0);
        out.println(res);
        out.flush();
    }

    static void dfs(int u, int ans) {
        res = Math.max(ans, res);
        used[u]++;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (used[j] < 2) dfs(j, ans + w[i]);
        }
        used[u]--;
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
